package com.toe.forum.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.common.restful.JsonPage;
import com.toe.pojo.dto.forum.UserLikeAddDTO;
import com.toe.pojo.dto.forum.UserLikeUpdateDTO;
import com.toe.pojo.entity.forum.ForumUserLike;
import com.toe.forum.webapi.mapper.ForumUserLikeMapper;
import com.toe.forum.service.ForumUserLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.pojo.vo.forum.UserLIkeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 点赞用户映射表 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@Slf4j
public class ForumUserLikeServiceImpl extends ServiceImpl<ForumUserLikeMapper, ForumUserLike> implements ForumUserLikeService {

    @Autowired
    private ForumUserLikeMapper forumUserLikeMapper;

    @Override
    public void insert(UserLikeAddDTO userLikeAddDTO) {
        //这里不用验证是否已经存在记录,在articleService中会进行处理
        Date now = new Date();
        ForumUserLike forumUserLike = new ForumUserLike();
        forumUserLike.setUserId(userLikeAddDTO.getUserId());
        forumUserLike.setArticleId(userLikeAddDTO.getArticleId());
        forumUserLike.setCreateTime(now);
        forumUserLike.setUpdateTime(now);
        forumUserLikeMapper.insert(forumUserLike);
    }

    @Override
    public void deleteById(Long id) {
        forumUserLikeMapper.deleteById(id);
    }

    @Override
    public void deleteUserLike(long userId, long articleId) {
        forumUserLikeMapper.deleteUserLike(userId, articleId);
    }

    @Override
    public void updateUserLike(UserLikeUpdateDTO userLikeUpdateDTO) {
        forumUserLikeMapper.updateUserLike(userLikeUpdateDTO);
    }

    @Override
    public List<UserLIkeVO> selectAllUserLike() {
        log.info("开始执行查询全部点赞文章映射的业务!");
        List<UserLIkeVO> userLIkeVOS = forumUserLikeMapper.selectAllUserLike();
        return userLIkeVOS;
    }

    @Override
    public JsonPage<UserLIkeVO> selectAllUserLikePage(Integer pageNum,Integer pageSize) {
        log.info("开始执行分页查询全部点赞文章映射的业务!");
        PageHelper.startPage(pageNum,pageSize);
        List<UserLIkeVO> userLIkeVOS = forumUserLikeMapper.selectAllUserLike();
        return JsonPage.restPage(new PageInfo<>(userLIkeVOS));
    }

    @Override
    public List<UserLIkeVO> selectAllUserLikeByArticleId(long articleId) {
        log.info("开始执行查询某个文章全部点赞文章映射的业务!");
        List<UserLIkeVO> userLIkeVOS = forumUserLikeMapper.selectAllUserLikeByArticleId(articleId);
        return userLIkeVOS;
    }

    @Override
    public JsonPage<UserLIkeVO> selectAllUserLikeByArticleIdPage(long articleId,Integer pageNum,Integer pageSize) {
        log.info("开始执行分页查询某个文章全部点赞文章映射的业务!");
        PageHelper.startPage(pageNum,pageSize);
        List<UserLIkeVO> userLIkeVOS = forumUserLikeMapper.selectAllUserLikeByArticleId(articleId);
        return JsonPage.restPage(new PageInfo<>(userLIkeVOS));
    }

    @Override
    public Boolean selectOneUserLike(long userId, long articleId) {
        log.info("开始执行查询某个点赞映射是否存在的业务");
        UserLIkeVO userLIkeVO = forumUserLikeMapper.selectOneUserLike(userId, articleId);
        if (userLIkeVO!=null)
            return true;
        return false;
    }
}
